package com.knowif.backend.controller;

import com.github.pagehelper.PageInfo;
import com.knowif.backend.service.SpecialService;
import com.knowif.pojo.Special;
import com.knowif.pojo.SpecialExample;
import com.knowif.search.SearchCondition;
import com.knowif.system.model.RestBean;
import com.knowif.util.ExampleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ：Xh
 * @date ：Created in 2019/6/5 0005 21:06
 */
@RestController
@RequestMapping("/special")
public class SpecialController {

    @Autowired
    private SpecialService specialService;

    @PostMapping("/save")
    public RestBean save(@RequestBody Special special){
        if (specialService.saveSpecial(special)){
            return RestBean.buildSuccess("OK");
        }else{
            return RestBean.buildError("添加失败");
        }
    }

    @PostMapping("/query")
    public RestBean<?> query(@RequestBody SearchCondition searchCondition){
        try {
            SpecialExample specialExample = ExampleUtil.buildExample(new SpecialExample(), searchCondition);
            List<Special> specials = specialService.selectSpecial(specialExample);
            PageInfo<Special> pageInfo = new PageInfo<Special>(specials);
            return RestBean.build(RestBean.success_code, "", pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.buildError("参数错误");
        }
    }

    @PostMapping("/queryNoPage")
    public RestBean<?> queryNoPage(@RequestBody SearchCondition searchCondition){
        try {
            SpecialExample specialExample = ExampleUtil.buildExample(new SpecialExample(), searchCondition);
            List<Special> specials = specialService.selectSpecial(specialExample, false);
            return RestBean.build(RestBean.success_code, "", specials);
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.buildError("参数错误");
        }
    }

    /**
     * 单个删除
     * @param specialId
     * @return
     */
    @DeleteMapping("/delete/{specialId}")
    public RestBean<?> delete(@PathVariable("specialId") int specialId){
        try {
            specialService.deleteSpecial(specialId);
            return RestBean.build(RestBean.success_code);
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.buildError("参数错误");
        }
    }

    /**
     * 批量删除
     * @param searchCondition
     * @return
     */
    @PostMapping("/deleteAll")
    public RestBean<?> deleteAll(@RequestBody SearchCondition searchCondition){
        try {
            SpecialExample specialExample = ExampleUtil.buildExample(new SpecialExample(), searchCondition);
            List<Special> specials = specialService.selectSpecial(specialExample, false);
            if(specials != null && specials.size() > 0){
                specials.forEach(special -> {
                    specialService.deleteSpecial(special.getId());
                });
            }
            return RestBean.build(RestBean.success_code);
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.buildError("参数错误");
        }
    }
}
